package com.onesports.intelligent.k12.polarlight.controller.organization.staff;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.onesports.framework.kit.common.valid.Json;
import com.onesports.intelligent.k12.polarlight.common.errors.exceptions.BusinessException;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.constant.BizConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.EditStatusDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.common.IdDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.OrganizationStaffDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.organization.OrganizationStaffQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.sys.RunAccountDTO;
import com.onesports.intelligent.k12.polarlight.domain.entity.organization.OrganizationStaff;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.Role;
import com.onesports.intelligent.k12.polarlight.domain.vo.organization.OrganizationStaffVO;
import com.onesports.intelligent.k12.polarlight.enums.OrganizationStaffTypeEnum;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.organization.OrganizationStaffService;
import com.onesports.intelligent.k12.polarlight.service.sys.AccountService;
import com.onesports.intelligent.k12.polarlight.service.sys.RoleService;
import com.onesports.intelligent.k12.polarlight.service.sys.RunAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

@Api(tags = "机构端-机构管理-员工管理")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.ORGANIZATION_STAFF)
public class StaffController {

    private final OrganizationStaffService organizationStaffService;
    private final AccountService accountService;
    private final RoleService roleService;

    @GetMapping("/page")
    @ApiOperation("分页查询")
    public ResponseEntity<IPage<OrganizationStaffVO>> page(OrganizationStaffQueryDTO dto){
        return ResultUtil.success(organizationStaffService.page(dto));
    }


    @ApiOperation("添加")
    @PostMapping("/add")
    public ResponseEntity<String> add(@Json @RequestBody OrganizationStaffDTO dto){
        dto.setOrganizationId(SecurityUtils.getOrganizationId());
        this.organizationStaffService.add(dto, OrganizationStaffTypeEnum.STAFF);
        return ResultUtil.success("添加成功!");
    }

    @ApiOperation("编辑")
    @PostMapping("/edit")
    public ResponseEntity<String> edit(@Json @RequestBody OrganizationStaffDTO dto){
        dto.setOrganizationId(SecurityUtils.getOrganizationId());
        this.organizationStaffService.edit(dto,true);
        return ResultUtil.success("操作成功!");
    }

    @GetMapping("/all-role")
    @ApiOperation("添加页获取所有角色")
    public ResponseEntity<List<Role>> allRole() {
        return ResultUtil.success(this.roleService.allRole(SecurityUtils.getOrganizationId()));
    }

    @PostMapping("/reset-pwd")
    @ApiOperation("重置密码")
    public ResponseEntity<String> resetPwd(@Json @RequestBody IdDTO dto) {
        OrganizationStaff staff = organizationStaffService.getById(dto.getId());
        if(ObjectUtils.isEmpty(staff)){
            throw new BusinessException("未查找到员工数据");
        }
        dto.setId(staff.getAccountId());
        this.accountService.resetPwd(dto);
        return ResultUtil.success("操作成功!");
    }


    @PostMapping("/update-status")
    @ApiOperation("启用/停用")
    public ResponseEntity<String> updateStatus(@Json @RequestBody EditStatusDTO dto) {
        OrganizationStaff staff = organizationStaffService.getById(dto.getId());
        if(ObjectUtils.isEmpty(staff)){
            throw new BusinessException("未查找到员工数据");
        }
        dto.setId(staff.getAccountId());
        this.accountService.updateStatus(dto);
        return ResultUtil.success("操作成功");
    }

    @GetMapping("/get-by-Phone")
    @ApiOperation("根据手机号获取员工信息")
    @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "string", paramType = "query")
    public ResponseEntity<OrganizationStaffVO> getByPhone(@NotBlank(message = "请输入手机号") String phone){
        return ResultUtil.success(organizationStaffService.getOrganizationStaffByPhone(phone,SecurityUtils.getOrganizationId()));
    }
}
